﻿Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Imports System.Diagnostics.Contracts

Namespace FTTK.Utilities
    ''' <summary>
    ''' Class utility to generate HashCode for Cache support.
    ''' </summary>
    ''' <remarks></remarks>
    Friend NotInheritable Class HashGenerator

        Private Sub New()
        End Sub


        ''' <summary>
        ''' Generates the key.
        ''' </summary>
        ''' <param name="apikey">The apikey.</param>
        ''' <param name="operationName">Name of the operation.</param>
        ''' <param name="requestUrl">The request URL.</param>
        ''' <param name="username">The username.</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GenerateKey(apikey As String,
                                            operationName As String,
                                            requestUrl As String,
                                            Optional username As String = Nothing) As String
            Contract.Requires(Of ArgumentNullException)(Not String.IsNullOrEmpty(apikey), "ApiKey must be not null")
            Contract.Requires(Of ArgumentNullException)(Not String.IsNullOrEmpty(operationName), "operationName must be not null")
            Contract.Requires(Of ArgumentNullException)(Not String.IsNullOrEmpty(requestUrl), "requestUrl must be not null")
            Dim hashString As String = Nothing
            If Not String.IsNullOrEmpty(username) Then
                hashString = String.Format("{0}_{1}_{2}", apikey, operationName, requestUrl.ToLower())
            Else
                hashString = String.Format("{0}_{1}_{2}_{3}", apikey, operationName, requestUrl.ToLower(), username)
            End If
            Return hashString
        End Function
    End Class
End Namespace
